package com.example.demo.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.Result;
import com.example.demo.entity.Articles;
import com.example.demo.entity.Goods;
import com.example.demo.mapper.ArticlesMapper;
import com.example.demo.mapper.GoodsMapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @author sean
 */
@RestController
@RequestMapping("/articles")
public class ArticlesController extends BaseController {

    @Resource
    ArticlesMapper articlesMapper;

    @PostMapping
    public Result<?> save(@RequestBody Articles articles) {
        articles.setCreateTime(new Date());
        return Result.success(articlesMapper.insert(articles));
    }

    @PutMapping
    public Result<?> update(@RequestBody Articles articles) {
        return Result.success(articlesMapper.updateById(articles));
    }

    @DeleteMapping("/{id}")
    public Result<?> delete(@PathVariable Long id) {
        return Result.success(articlesMapper.deleteById(id));
    }

    @GetMapping("/{id}")
    public Result<?> getById(@PathVariable Long id) {
        return Result.success(articlesMapper.selectById(id));
    }

    @GetMapping("/all")
    public Result<?> findAll() {
        return Result.success(articlesMapper.selectList(null));
    }

    @GetMapping
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") String name) {
        Page<Articles> page = articlesMapper.findPage(new Page<>(pageNum, pageSize), name);
        return Result.success(page);
    }

}
